/***************************************************************
 * Name:      PitchPerfectorMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Ramon van Dam and Susan Laraghy ()
 * Created:   2009-03-09
 * Copyright: Ramon van Dam and Susan Laraghy ()
 * License:
 **************************************************************/

#include "wx_pch.h"
#include "PitchPerfectorMain.h"
#include <wx/msgdlg.h>
#include "playground.h"
#include "SongProperties.h"
#include "recordengine.h"
#include "engine.h"

//(*InternalHeaders(PitchPerfectorFrame)
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(PitchPerfectorFrame)
const long PitchPerfectorFrame::ID_STATICTEXT1 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT2 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT3 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT4 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT7 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT8 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT5 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT6 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT9 = wxNewId();
const long PitchPerfectorFrame::ID_CHOICE1 = wxNewId();
const long PitchPerfectorFrame::ID_STATICTEXT11 = wxNewId();
const long PitchPerfectorFrame::ID_SPINCTRL1 = wxNewId();
const long PitchPerfectorFrame::ID_BUTTON1 = wxNewId();
const long PitchPerfectorFrame::ID_GAUGE1 = wxNewId();
const long PitchPerfectorFrame::ID_TABVIEW = wxNewId();
const long PitchPerfectorFrame::ID_SCROLLBAR1 = wxNewId();
const long PitchPerfectorFrame::ID_TEXTCTRL1 = wxNewId();
const long PitchPerfectorFrame::ID_PANEL1 = wxNewId();
const long PitchPerfectorFrame::idMenuPlayground = wxNewId();
const long PitchPerfectorFrame::idMenuQuit = wxNewId();
const long PitchPerfectorFrame::idMenuLoadSong = wxNewId();
const long PitchPerfectorFrame::idMenuPlaySong = wxNewId();
const long PitchPerfectorFrame::idMenuStopSong = wxNewId();
const long PitchPerfectorFrame::idMenuSongProperties = wxNewId();
const long PitchPerfectorFrame::idMenuAbout = wxNewId();
const long PitchPerfectorFrame::ID_STATUSBAR1 = wxNewId();
const long PitchPerfectorFrame::idToolLoadSong = wxNewId();
const long PitchPerfectorFrame::idToolPlaySong = wxNewId();
const long PitchPerfectorFrame::idToolStopSong = wxNewId();
const long PitchPerfectorFrame::idToolSwitchSound = wxNewId();
const long PitchPerfectorFrame::ID_TOOLBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(PitchPerfectorFrame,wxFrame)
    //(*EventTable(PitchPerfectorFrame)
    //*)
END_EVENT_TABLE()

PitchPerfectorFrame::PitchPerfectorFrame(wxWindow* parent,wxWindowID id)
{
    // own initialization
    theSong = new song();
    theEngine = new engine(theSong, 0);

    //(*Initialize(PitchPerfectorFrame)
    wxBoxSizer* BoxSizer4;
    wxStaticBoxSizer* StaticBoxSizer2;
    wxBoxSizer* BoxSizer5;
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxStaticBoxSizer* StaticBoxSizer1;
    wxFlexGridSizer* FlexGridSizer1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, _("PitchPerfector"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, Panel1, _("Song"));
    FlexGridSizer1 = new wxFlexGridSizer(0, 4, 0, 0);
    FlexGridSizer1->AddGrowableCol(1);
    FlexGridSizer1->AddGrowableCol(3);
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("Title:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    FlexGridSizer1->Add(StaticText1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txtSongTitle = new wxStaticText(Panel1, ID_STATICTEXT2, _("..."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    FlexGridSizer1->Add(txtSongTitle, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText3 = new wxStaticText(Panel1, ID_STATICTEXT3, _("Artist:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    FlexGridSizer1->Add(StaticText3, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txtSongArtist = new wxStaticText(Panel1, ID_STATICTEXT4, _("..."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
    FlexGridSizer1->Add(txtSongArtist, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText4 = new wxStaticText(Panel1, ID_STATICTEXT7, _("Tempo:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT7"));
    FlexGridSizer1->Add(StaticText4, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txtSongTempo = new wxStaticText(Panel1, ID_STATICTEXT8, _("..."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT8"));
    FlexGridSizer1->Add(txtSongTempo, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT5, _("Meter:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT5"));
    FlexGridSizer1->Add(StaticText2, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txtSongMeter = new wxStaticText(Panel1, ID_STATICTEXT6, _("..."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT6"));
    FlexGridSizer1->Add(txtSongMeter, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText6 = new wxStaticText(Panel1, ID_STATICTEXT9, _("Speed:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT9"));
    FlexGridSizer1->Add(StaticText6, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    choSpeed = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
    choSpeed->Append(_("0.25 x normal speed"));
    choSpeed->Append(_("0.5   x normal speed"));
    choSpeed->SetSelection( choSpeed->Append(_("1      x normal speed")) );
    choSpeed->Append(_("1.5   x normal speed"));
    choSpeed->Append(_("2      x normal speed"));
    choSpeed->Append(_("4      x normal speed"));
    FlexGridSizer1->Add(choSpeed, 1, wxBOTTOM|wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText5 = new wxStaticText(Panel1, ID_STATICTEXT11, _("Treshold:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT11"));
    FlexGridSizer1->Add(StaticText5, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    spnTreshold = new wxSpinCtrl(Panel1, ID_SPINCTRL1, _T("500"), wxDefaultPosition, wxSize(66,21), 0, 0, 1000000, 500, _T("ID_SPINCTRL1"));
    spnTreshold->SetValue(_T("500"));
    BoxSizer5->Add(spnTreshold, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    butDetectTreshold = new wxButton(Panel1, ID_BUTTON1, _("Detect automatically"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    butDetectTreshold->Disable();
    BoxSizer5->Add(butDetectTreshold, 0, wxBOTTOM|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer5, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 0);
    StaticBoxSizer1->Add(FlexGridSizer1, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 0);
    BoxSizer2->Add(StaticBoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticBoxSizer2 = new wxStaticBoxSizer(wxHORIZONTAL, Panel1, _("Music"));
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    gauMonitor = new wxGauge(Panel1, ID_GAUGE1, 100, wxDefaultPosition, wxDefaultSize, wxGA_VERTICAL, wxDefaultValidator, _T("ID_GAUGE1"));
    BoxSizer4->Add(gauMonitor, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    tabViewCanvas = new tabView(Panel1, theSong, theEngine);
    BoxSizer3->Add(tabViewCanvas, 1, wxTOP|wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    scrlTabView = new wxScrollBar(Panel1, ID_SCROLLBAR1, wxDefaultPosition, wxDefaultSize, wxSB_HORIZONTAL, wxDefaultValidator, _T("ID_SCROLLBAR1"));
    scrlTabView->SetScrollbar(0, 1, 10, 1);
    BoxSizer3->Add(scrlTabView, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4->Add(BoxSizer3, 1, wxALL|wxEXPAND|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 0);
    StaticBoxSizer2->Add(BoxSizer4, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    BoxSizer2->Add(StaticBoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    txtLog = new wxTextCtrl(Panel1, ID_TEXTCTRL1, _("Debug window\n"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    txtLog->SetBackgroundColour(wxColour(252,255,193));
    BoxSizer2->Add(txtLog, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(BoxSizer2);
    BoxSizer2->Fit(Panel1);
    BoxSizer2->SetSizeHints(Panel1);
    BoxSizer1->Add(Panel1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    menuPlayground = new wxMenuItem(Menu1, idMenuPlayground, _("Playground\tCtrl+0"), wxEmptyString, wxITEM_NORMAL);
    Menu1->Append(menuPlayground);
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu3 = new wxMenu();
    menuLoadSong = new wxMenuItem(Menu3, idMenuLoadSong, _("Load song\tCtrl+l"), _("Load a song"), wxITEM_NORMAL);
    Menu3->Append(menuLoadSong);
    menuSongPlay = new wxMenuItem(Menu3, idMenuPlaySong, _("Play song\tCtrl+p"), _("Play the song"), wxITEM_NORMAL);
    Menu3->Append(menuSongPlay);
    menuSongPlay->Enable(false);
    MenuItem5 = new wxMenuItem(Menu3, idMenuStopSong, _("Stop song"), _("Stopt the song"), wxITEM_NORMAL);
    Menu3->Append(MenuItem5);
    MenuItem5->Enable(false);
    menuSongProperties = new wxMenuItem(Menu3, idMenuSongProperties, _("Properties\tCtrl+q"), _("Display the song properties"), wxITEM_NORMAL);
    Menu3->Append(menuSongProperties);
    MenuBar1->Append(Menu3, _("Song"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    ToolBar1 = new wxToolBar(this, ID_TOOLBAR1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxNO_BORDER, _T("ID_TOOLBAR1"));
    ToolBarItem1 = ToolBar1->AddTool(idToolLoadSong, _("Load song"), wxBitmap(wxImage(_T("images\\music.png"))), wxBitmap(wxImage(_T("images\\music_disabled.png"))), wxITEM_NORMAL, wxEmptyString, wxEmptyString);
    ToolBarItem2 = ToolBar1->AddTool(idToolPlaySong, _("Play song"), wxBitmap(wxImage(_T("images\\control_play_blue.png"))), wxBitmap(wxImage(_T("images\\control_play_blue_disabled.png"))), wxITEM_NORMAL, _("Play song"), _("Play song"));
    ToolBarItem3 = ToolBar1->AddTool(idToolStopSong, _("Stop song"), wxBitmap(wxImage(_T("images\\control_stop_blue.png"))), wxBitmap(wxImage(_T("images\\control_stop_blue_disabled.png"))), wxITEM_NORMAL, _("Stop song"), _("Stop song"));
    ToolBarItem4 = ToolBar1->AddTool(idToolSwitchSound, _("Switch sound on/off"), wxBitmap(wxImage(_T("images\\sound.png"))), wxNullBitmap, wxITEM_CHECK, _("Switch sound on/off"), _("Switch sound on/off"));
    ToolBar1->Realize();
    SetToolBar(ToolBar1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnChoSpeedSelect);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PitchPerfectorFrame::OnButDetectTresholdClick);
    Connect(ID_SCROLLBAR1,wxEVT_SCROLL_THUMBTRACK,(wxObjectEventFunction)&PitchPerfectorFrame::OnScrlTabViewScrollThumbTrack);
    Connect(ID_SCROLLBAR1,wxEVT_SCROLL_CHANGED,(wxObjectEventFunction)&PitchPerfectorFrame::OnScrlTabViewScrollChanged);
    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&PitchPerfectorFrame::OntxtLogText);
    Connect(idMenuPlayground,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnMenuPlaygroundSelected);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnQuit);
    Connect(idMenuLoadSong,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnMenuLoadSongSelected);
    Connect(idMenuPlaySong,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnMenuSongPlaySelected);
    Connect(idMenuSongProperties,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnMenuSongPropertiesSelected);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&PitchPerfectorFrame::OnAbout);
    Connect(idToolLoadSong,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&PitchPerfectorFrame::OnToolLoadSongClicked);
    Connect(idToolPlaySong,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&PitchPerfectorFrame::ONtoolPlayClicked);
    Connect(idToolStopSong,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&PitchPerfectorFrame::OnToolStopSongClicked);
    Connect(idToolSwitchSound,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&PitchPerfectorFrame::OnToolSwitchSoundClicked);
    //*)

    GetToolBar()->ToggleTool(idToolSwitchSound, false);
    theSong->speed = 1;
    theEngine->theTabView = tabViewCanvas;
    theEngine->theLog = txtLog;
    theEngine->theGauge = gauMonitor;
    tabViewCanvas->theGauge = gauMonitor;
    theEngine->spnTreshold = spnTreshold;
    tabViewCanvas->theLog = txtLog;
    tabViewCanvas->theScrollBar = scrlTabView;
    scrlTabView->Disable();
    updateToolBar();
}

PitchPerfectorFrame::~PitchPerfectorFrame()
{
    //(*Destroy(PitchPerfectorFrame)
    //*)
    if(theSong)
      delete theSong;
    if(theEngine)
      delete theEngine;
}

void PitchPerfectorFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void PitchPerfectorFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void PitchPerfectorFrame::OnMenuPlaygroundSelected(wxCommandEvent& event)
{
  Playground dlgPlayground(this);
  dlgPlayground.ShowModal();
}

// test function, very ugly code ;)
void PitchPerfectorFrame::loadSong() {
  wxString path;
  wxString FILETYPES = _T("Songs (*.xml)|*.xml|All files|*.*");
  wxFileDialog* openFileDialog = new wxFileDialog(this, _T("Open song file"), wxEmptyString, wxEmptyString,
                                                  FILETYPES, wxOPEN, wxDefaultPosition);
	if(openFileDialog->ShowModal() == wxID_OK ) {
	  theSong->reset();
    theSong->loadSong(openFileDialog->GetPath());
    theEngine->reset();
    txtSongTitle->SetLabel(theSong->title);
    txtSongArtist->SetLabel(theSong->artist);
    txtSongTempo->SetLabel(wxString::Format(_("%d"), theSong->tempo));
    txtSongMeter->SetLabel(wxString::Format(_("%d"), theSong->metertop) + _("/") + wxString::Format(_("%d"), theSong->meterbottom));
    updateToolBar();
    txtLog->AppendText(wxString::Format(_("Total length: %2.3f counts\n"), theSong->totalLength));
    scrlTabView->Enable();
    butDetectTreshold->Enable();
    tabViewCanvas->resizeScrollBar();
    tabViewCanvas->Refresh();
    Update();
    Refresh();
    Update();
    Refresh();
	}
}

void PitchPerfectorFrame::OnMenuLoadSongSelected(wxCommandEvent& event)
{
  loadSong();
}

void PitchPerfectorFrame::OnToolLoadSongClicked(wxCommandEvent& event)
{
  loadSong();
}


void PitchPerfectorFrame::ONtoolPlayClicked(wxCommandEvent& event)
{
  playSong();
}

void PitchPerfectorFrame::OnMenuSongPlaySelected(wxCommandEvent& event)
{
  playSong();
}

void PitchPerfectorFrame::playSong() {
  gauMonitor->SetValue(100);
  theEngine->start();
  updateToolBar();
}

void PitchPerfectorFrame::OnMenuSongPropertiesSelected(wxCommandEvent& event)
{
  SongProperties dlgSongProperties(this);
  dlgSongProperties.theSong = theSong;
  dlgSongProperties.showProperties();
  dlgSongProperties.ShowModal();
}

void PitchPerfectorFrame::OnToolSwitchSoundClicked(wxCommandEvent& event)
{
  theEngine->sound = GetToolBar()->GetToolState(idToolSwitchSound);
  GetToolBar()->ToggleTool(idToolSwitchSound, theEngine->sound);
}

void PitchPerfectorFrame::OntxtLogText(wxCommandEvent& event)
{
  // nothing
}

void PitchPerfectorFrame::updateToolBar() {
  GetToolBar()->EnableTool(idToolPlaySong, theSong->artist.Length() > 0 && !theEngine->playing);
  GetToolBar()->EnableTool(idToolStopSong, theEngine->playing);
  GetToolBar()->EnableTool(idToolLoadSong, !theEngine->playing);
}

void PitchPerfectorFrame::stopSong() {
  theEngine->stop();
  updateToolBar();
}

void PitchPerfectorFrame::OnToolStopSongClicked(wxCommandEvent& event)
{
  stopSong();
}

void PitchPerfectorFrame::OnScrlTabViewScrollThumbTrack(wxScrollEvent& event)
{
  tabViewCanvas->handleScollBar();
}

void PitchPerfectorFrame::OnScrlTabViewScrollChanged(wxScrollEvent& event)
{
  tabViewCanvas->handleScollBar();
}

void PitchPerfectorFrame::OnButDetectTresholdClick(wxCommandEvent& event)
{
  recordEngine *testRecordEngine = new recordEngine(440, 1000, txtLog, 0, theSong->notes[0], tabViewCanvas, gauMonitor, 1, spnTreshold);
  if(testRecordEngine->Create() != wxTHREAD_NO_ERROR) {
    wxMessageBox(_("Can't create thread!"));
  }
  wxMessageBox(_("After you click the button, please play an A440"));
  if(testRecordEngine->Run() != wxTHREAD_NO_ERROR) {
    wxMessageBox(_("Can't start thread!"));
  }
}

void PitchPerfectorFrame::OnChoSpeedSelect(wxCommandEvent& event)
{
  switch(choSpeed->GetSelection()) {
    case 0:
      theSong->speed = 0.25;
      break;
    case 1:
      theSong->speed = 0.5;
      break;
    case 2:
      theSong->speed = 1;
      break;
    case 3:
      theSong->speed = 1.5;
      break;
    case 4:
      theSong->speed = 2;
      break;
    case 5:
      theSong->speed = 4;
      break;
  }
  theEngine->reset();
}
